using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using PetriDiagram;
using PetriDiagram.Model;
using Diagram.Model;
using System;
using PetriDiagram.Algorithms;

namespace PetriOwl
{
    public partial class OwlForm : Form
    {
        private PetriDocument document;
        public OwlForm()
        {
            InitializeComponent();

            this.document = new PetriDocument();

            toolBox.Diagram = document.Diagram;
            diagramControl.Diagram = toolBox.Diagram;
        }

        private void coveringTreeToolStripMenuItem_Click( object sender, EventArgs e )
        {
            try
            {
                CoveringTreeForm.ShowDialog( this.document.PetriModel.IncidentionMatrix,
                    this.document.PetriModel.GetMarkingVector(),
                    this.document.PetriModel );
            }
            catch( Exception ex )
            {
                MessageBox.Show( ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void stateSpaceAnalysisToolStripMenuItem_Click( object sender, EventArgs e )
        {
            try
            {
                SSAnalysisForm form = new SSAnalysisForm( this.document );
                form.ShowDialog();
            }
            catch( Exception ex )
            {
                MessageBox.Show( ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void invariantsToolStripMenuItem_Click( object sender, EventArgs e )
        {
            try
            {
                InvariantsForm frm = new InvariantsForm( document.PetriModel );
                frm.Matrix = document.PetriModel.IncidentionMatrix;
                frm.ShowDialog();
            }
            catch( Exception ex )
            {
                MessageBox.Show( ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void saveAsToolStripMenuItem_Click( object sender, EventArgs e )
        {
            SaveFileDialog dlg = new SaveFileDialog();

            if( dlg.ShowDialog() == DialogResult.OK )
            {
                using( FileStream file = new FileStream( dlg.FileName, FileMode.Create ) )
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize( file, document );
                    file.Close();
                }
            }
        }

        private void openToolStripMenuItem_Click( object sender, EventArgs e )
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if( dlg.ShowDialog() == DialogResult.OK )
            {
                using( FileStream file = new FileStream( dlg.FileName, FileMode.Open ) )
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    document = bf.Deserialize( file ) as PetriDocument;
                    diagramControl.Diagram = document.Diagram;
                    document.Diagram.ParentControl = diagramControl;
                    toolBox.Diagram = document.Diagram;
                    file.Close();
                }
            }
        }
    }
}